#include "QBFlags.h"

#include <qbprotocol/include/SSQBModel.h>

const std::string CQBFlags::CommonPageAddedFlag_Key =
    std::string("CommonPageAddedKey");
const std::string CQBFlags::CommonPageAddedFlag_Val = std::string("Added");

void CQBFlags::Update(const xQBFlagsAck_c *pdata) {
  if (!pdata) return;

  for (auto itr = pdata->listFlags.begin(); itr != pdata->listFlags.end();
       ++itr) {
    const xQbFlag_c &flag = *itr;
    m_data.insert(
        make_pair(std::string(flag.szKey), std::string(flag.szValue)));
  }

  m_bFlagInitialized = true;
}

void CQBFlags::Update(const xQbFlagAdd_c *pdata) {
  if (strlen(pdata->flag_.szKey) > 0)
    m_data.insert(make_pair(std::string(pdata->flag_.szKey),
                            std::string(pdata->flag_.szValue)));
}

void CQBFlags::Clear() { m_data.clear(); }

bool CQBFlags::GetValue(const char *pkey, std::string &value) {
  if (pkey == nullptr) return false;

  auto itrFind = m_data.find(std::string(pkey));
  if (itrFind != m_data.end()) {
    value = itrFind->second;
    return true;
  }

  return false;
}